<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="商户">
                <a-input v-model="queryParam.merchantKeyword" placeholder="" />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="处理人">
                <a-input v-model="queryParam.handleUserName" placeholder="" />
              </a-form-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="8" :sm="24">
                <a-form-item label="申请类型">
                  <a-select v-model="queryParam.applyType" placeholder="请选择">
                    <a-select-option value="1">申请合作</a-select-option>
                    <a-select-option value="2">申请定制模板</a-select-option>
                    <a-select-option value="3">申请购买模板</a-select-option>
                    <a-select-option value="4">申请修改模板</a-select-option>
                    <a-select-option value="5">申请账单介入</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="状态">
                  <a-select v-model="queryParam.status" placeholder="请选择">
                    <a-select-option value="0">未处理</a-select-option>
                    <a-select-option value="1">已处理</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="申请开始时间">
                  <a-date-picker v-model="queryParam.startCreateTime" style="width: 100%" placeholder="申请开始时间" />
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="申请结束时间">
                  <a-date-picker v-model="queryParam.endCreateTime" style="width: 100%" placeholder="申请结束时间" />
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="处理开始时间">
                  <a-date-picker v-model="queryParam.startHandleTime" style="width: 100%" placeholder="处理开始时间" />
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="处理结束时间">
                  <a-date-picker v-model="queryParam.endHandleTime" style="width: 100%" placeholder="处理结束时间" />
                </a-form-item>
              </a-col>
            </template>
            <a-col :md="8" :sm="24">
              <span
                class="table-page-search-submitButtons"
                :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
              >
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => (this.queryParam = {})">重置</a-button>
                <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'" />
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <s-table
        ref="table"
        size="default"
        rowKey="applyId"
        :columns="columns"
        :data="loadData"
        :alert="false"
        showPagination="auto"
        listName="dataList"
        :scroll="{ x: 'calc(1000px + 100%)' }"
        v-if="btnsPurviewData.query.isShow"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
        <div slot="merchantInfo" slot-scope="text, record">
          <div>{{ record.merchantName }}</div>
          <div>
            <span>{{ record.contactName }}</span
            >&nbsp;&nbsp;<span>{{ record.contactMobile }}</span>
          </div>
        </div>
        <div slot="applyUserInfo" slot-scope="text, record">
          <div>{{ record.applyUserName }}</div>
          <div>{{ record.applyUserMobile }}</div>
        </div>
        <span slot="status" slot-scope="text">
          {{ text | statusFilter }}
        </span>
        <span slot="applyType" slot-scope="text">
          {{ text | applyTypeFilter }}
        </span>
        <span slot="action" slot-scope="text, record">
          <template v-if="btnsPurviewData.audit.isShow">
            <a @click="_showHandleForm(record)" v-if="!record.handleResult"> 标记处理 </a>
            <span class="handleMarkDisabled" v-else>标记处理</span>
          </template>
        </span>
      </s-table>
      <create-handle-form
        ref="createHandleForm"
        :visible="handleVisible"
        :loading="confirmLoading"
        :model="mdl"
        @cancel="_cancelMark"
        @ok="_handleMark"
      >
      </create-handle-form>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { BTNS_PURVIEW_MAP } from '@/store/mutation-types'
import storage from 'store'
import { STable, Ellipsis } from '@/components'
import { getMerchantBusApplyList, handleMark } from '@/api/merchantManage'
import CreateHandleForm from './modules/CreateHandleForm'
const columns = [
  {
    title: '序号',
    scopedSlots: { customRender: 'serial' },
    align: 'center',
    fixed: 'left',
    width: 80
  },
  {
    title: '商户信息',
    scopedSlots: { customRender: 'merchantInfo' },
    align: 'center'
  },
  {
    title: '申请人信息',
    scopedSlots: { customRender: 'applyUserInfo' },
    align: 'center'
  },
  {
    title: '申请类型',
    dataIndex: 'applyType',
    scopedSlots: { customRender: 'applyType' },
    align: 'center'
  },
  {
    title: '申请内容',
    customRender: (record) => {
      if (record.applyType <= 2) {
        return '—'
      } else {
        const objJson = JSON.parse(record.objJson)
        console.log(objJson)
        if (record.applyType === 5) {
          return `合同编号：${objJson.contractNo}`
        } else {
          return `模板编号：${objJson.templateNo}`
        }
      }
    },
    align: 'center'
  },
  {
    title: '状态',
    dataIndex: 'status',
    scopedSlots: { customRender: 'status' },
    align: 'center'
  },
  {
    title: '处理结果',
    dataIndex: 'handleResult',
    customRender: (text) => text ?? '—',
    align: 'center'
  },
  {
    title: '处理时间',
    dataIndex: 'handleTime',
    customRender: (text) => text ?? '—',
    align: 'center'
  },
  {
    title: '处理人',
    dataIndex: 'handleUserName',
    customRender: (text) => text ?? '—',
    align: 'center'
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    customRender: (text) => text || '—',
    align: 'center'
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '180px',
    scopedSlots: { customRender: 'action' },
    align: 'center',
    fixed: 'right'
  }
]

const statusMap = {
  0: {
    text: '未处理'
  },
  1: {
    text: '已处理'
  }
}

const applyTypeMap = {
  1: {
    text: '申请合作'
  },
  2: {
    text: '申请定制模板'
  },
  3: {
    text: '申请购买模板'
  },
  4: {
    text: '申请修改模板'
  },
  5: {
    text: '申请账单介入'
  }
}

export default {
  name: 'FranchiseeList',
  components: {
    STable,
    Ellipsis,
    CreateHandleForm
  },
  data () {
    this.columns = columns
    return {
      btnsPurviewMap: storage.get(BTNS_PURVIEW_MAP),
      btnsPurviewData: {},
      // create model
      visible: false,
      handleVisible: false,
      confirmLoading: false,
      mdl: null,
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        return getMerchantBusApplyList(requestParameters)
          .then((res) => {
            return res.result
          })
          .catch((e) => {})
      },
      selectedRowKeys: [],
      selectedRows: [],
      repaymentPlanSelList: []
    }
  },
  filters: {
    statusFilter (type) {
      return statusMap[type].text
    },
    applyTypeFilter (type) {
      return applyTypeMap[type].text
    }
  },
  created () {
    this.btnsPurviewData = this.btnsPurviewMap[this.$route.meta.permission[0]]
  },
  computed: {},
  methods: {
    /**
     * 取消标记
     */
    _cancelMark () {
      this.handleVisible = false
      const form = this.$refs.createHandleForm.handleForm
      form.resetFields() // 清理表单数据（可不做）
    },
    /**
     * 标记处理
     */
    _handleMark () {
      const form = this.$refs.createHandleForm.handleForm
      this.confirmLoading = true
      form.validateFields((errors, values) => {
        if (!errors) {
          handleMark({
            handleResult: values.handleResult,
            applyId: values.applyId
          })
            .then((res) => {
              this.handleVisible = false
              this.confirmLoading = false
              this.$refs.table.refresh()
              this.$message.info(`标记成功`)
            })
            .catch((e) => {
              this.confirmLoading = false
            })
        } else {
          this.confirmLoading = false
        }
      })
    },
    /**
     * 切换searchBar展开/关闭
     */
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    /**
     * 切换商户状态
     */
    _showHandleForm (record) {
      this.handleVisible = true
      this.mdl = { applyId: record.applyId, handleResult: '' }
    }
  }
}
</script>
<style lang="less" scope>
.handleMarkDisabled {
  cursor: not-allowed;
}
</style>
